package com.desgin.observer;

import java.util.ArrayList;
import java.util.Collections;

public class StatisticsDisplay implements DisplayElement, Observer{
    private Subject weatherData;

    private ArrayList<Float> tempList = new ArrayList<>();
    private ArrayList<Float> humList = new ArrayList<>();
    private ArrayList<Float> presList = new ArrayList<>();

    public StatisticsDisplay(Subject weatherData){
        this.weatherData = weatherData;
        //将自己注册
        weatherData.registerObserver(this);
    }

    @Override
    public void display() {
        System.out.println("maximun temperature:" + Collections.max(tempList));
        System.out.println("minimun temperature:" + Collections.min(tempList));
        System.out.println("avg: " + getArrayListAvg(tempList));
    }

    @Override
    public void update(float temp, float humidity, float pressure) {
        tempList.add(temp);
        humList.add(humidity);
        presList.add(pressure);
        display();
    }

    private float getArrayListAvg(ArrayList<Float> arrayList){
        float sum = 0.0f;
        for (int i = 0; i < arrayList.size(); i++) {
           sum += arrayList.get(i);
        }
        return sum/arrayList.size();
    }
}
